package com.ruoyi.stock2.controller;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stock2.domain.ForetellResult;
import com.ruoyi.stock2.service.IForetellResultService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 交易预测Controller
 * 
 * @author ruoyi
 * @date 2020-04-08
 */
@Controller
@RequestMapping("/stock2/RESULT")
public class ForetellResultController extends BaseController
{
    private String prefix = "stock2/RESULT";

    @Autowired
    private IForetellResultService foretellResultService;

    @RequiresPermissions("stock2:RESULT:view")
    @GetMapping()
    public String RESULT()
    {
        return prefix + "/RESULT";
    }

    /**
     * 查询交易预测列表
     */
    @RequiresPermissions("stock2:RESULT:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ForetellResult foretellResult)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
            System.out.println("pageNum"+pageNum);
            System.out.println("pageSize"+pageSize);
            System.out.println("orderBy"+orderBy);
        }
        List<ForetellResult> list = foretellResultService.selectForetellResultList(foretellResult);
        return getDataTable(list);
    }

    /**
     * 导出交易预测列表
     */
    @RequiresPermissions("stock2:RESULT:export")
    @Log(title = "交易预测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ForetellResult foretellResult)
    {

        PageDomain pageDomain = TableSupport.buildPageRequest();
        List<ForetellResult> lists = new ArrayList<ForetellResult>(10000);
        Integer pageNum = 1;
        Integer pageSize = 500;
        while(true){
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());

            PageHelper.startPage(pageNum, pageSize, orderBy);
            List<ForetellResult> list = foretellResultService.selectForetellResultList(foretellResult);
            lists.addAll(list);
            pageNum++;

            if(list.size()<500){
                break;
            }
            System.out.println("pageNum"+pageNum);
            System.out.println("pageSize"+pageSize);
            System.out.println("orderBy"+orderBy);
            System.out.println("list.size()"+list.size());
            System.out.println("list.1()"+list.get(0).toString());
        }


        ExcelUtil<ForetellResult> util = new ExcelUtil<ForetellResult>(ForetellResult.class);
        return util.exportExcel(lists, "RESULT");
    }

    /**
     * 新增交易预测
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存交易预测
     */
    @RequiresPermissions("stock2:RESULT:add")
    @Log(title = "交易预测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ForetellResult foretellResult)
    {
        return toAjax(foretellResultService.insertForetellResult(foretellResult));
    }

    /**
     * 修改交易预测
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ForetellResult foretellResult = foretellResultService.selectForetellResultById(id);
        mmap.put("foretellResult", foretellResult);
        return prefix + "/edit";
    }

    /**
     * 修改保存交易预测
     */
    @RequiresPermissions("stock2:RESULT:edit")
    @Log(title = "交易预测", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ForetellResult foretellResult)
    {
        return toAjax(foretellResultService.updateForetellResult(foretellResult));
    }

    /**
     * 删除交易预测
     */
    @RequiresPermissions("stock2:RESULT:remove")
    @Log(title = "交易预测", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(foretellResultService.deleteForetellResultByIds(ids));
    }
}
